package com.dllo.lanou.cookapp.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bmob.BmobProFile;
import com.bmob.btp.callback.DownloadListener;
import com.dllo.lanou.cookapp.R;
import com.dllo.lanou.cookapp.entity.AddCookStepEntity;
import com.dllo.lanou.cookapp.entity.Post;
import com.dllo.lanou.cookapp.entity.User;
import com.dllo.lanou.cookapp.volly.GetVolleyImage;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bmob.v3.datatype.BmobFile;

/**
 * Created by xijinhua on 2016/3/29.
 */
public class ShowCookingAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater mInflater;
    private List<AddCookStepEntity> list;

    public ShowCookingAdapter(Context context,List<AddCookStepEntity> list){
        this.context = context;
        this.list = list;
        mInflater = LayoutInflater.from(context);
    }

    public ShowCookingAdapter(Context context){
        this.context = context;
        mInflater = LayoutInflater.from(context);


    }
    public void  addData(List<AddCookStepEntity> list){

        if (this.list == null){
            this.list = new ArrayList<>();
        }
//        this.list.clear();
//        this.list.addAll(list);
                this.list = list;
        Log.i("addCookStepEntities",list.size()+"");
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list!= null && list.size()>0 ?list.size():0;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null){
            convertView =mInflater.inflate(R.layout.item_showcooking,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();

        }
        //加载数据
        AddCookStepEntity addCookStepEntity =   list.get(position);

        Log.i("addCookStepEntities",addCookStepEntity.getName()+"");

        if (addCookStepEntity.getName() !=null){
            holder.show_cooking_cookname.setText(addCookStepEntity.getName());
        }
        if (addCookStepEntity.getBmob_img() != null){
           GetVolleyImage.getNetWorkImg(addCookStepEntity.getBmob_img(),holder.show_cooking_img);
        }
        User  user = addCookStepEntity.getAuthor();
        holder.username.setText(user.getUsername());
        if (user.getImg() != null) {
            Log.i("图片",user.getImg()+"");
            user.getImg().loadImage(context, holder.show_cooking_head);
        }
//        }

        return convertView;
    }

    static class ViewHolder{
        @Bind(R.id.show_cooking_head)
        ImageView show_cooking_head;
        @Bind(R.id.show_cooking_img)
        ImageView show_cooking_img;
        @Bind(R.id.show_cooking_username)
        TextView username;
        @Bind(R.id.show_cooking_cookname)
        TextView  show_cooking_cookname;


        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }



}

